package com.iggy.at.facade.messages
{
	import com.iggy.at.api.Twitter;
	import com.iggy.at.api.data.TwitterDirectMessage;
	import com.iggy.at.facade.messages.TwitterDirectMessages;
	import com.iggy.at.api.data.TwitterStatus;
	import com.iggy.at.api.data.TwitterUser;
	import com.iggy.at.api.events.TwitterEvent;
	import mx.collections.ArrayCollection;
	
    public class TwitterDirectMessages
	{
		private var tw:Twitter;
		private var _userDetails:TwitterUserVO;
		private var _logedUserProfile:TwitterUser;
		private var _timelineDP:ArrayCollection;
	
		public function get userDetails():TwitterUserVO
		{
			return _userDetails;
		}
		public function get logedUserProfile():TwitterUser
		{
			return _logedUserProfile;
		}
		
		public function get timelineDP():ArrayCollection
		{
			return _timelineDP;
		}
		
		public function TwitterDirectMessages()
		{
			tw = new Twitter();
			tw.addEventListener( TwitterEvent.ON_GET_DIRECT_MESSAGES, loadDirectMessages );
			_timelineDP = new ArrayCollection();
			
			
		}
		
		
		public function loadTheDirectMessages(user:String, pass:String):void
		{
				if( user == "" )
			return;
			tw.setAuthenticationCredentials(user, pass);			
			tw.loadDirectMessages();
		}
		
		// called when user timeline is returned from webservices
		private function loadDirectMessages( evt:TwitterEvent ):void
		{
		// access timeline messages array
		var timeline:Array = evt.data as Array;
		// clear previous messages list contents
		_timelineDP.removeAll();
		// if no timeline found for specified user
		// exit function execution
		// user probably doesn't exists
			if(timeline == null || timeline.length == 0 )
				{
				return;
				}
		var twitMessage:TwitterDirectMessage; //TwitterStatus used to loop
			for each( twitMessage in timeline )
				{
				_timelineDP.addItem( twitMessage );
				}
		_logedUserProfile = twitMessage.user;
		_userDetails = new TwitterUserVO();
		_userDetails.source = logedUserProfile.profileImageUrl;
		_userDetails.name = logedUserProfile.name;
		_userDetails.screenName = logedUserProfile.screenName;
		_userDetails.location = logedUserProfile.location;
		_userDetails.url = logedUserProfile.url;
		_userDetails.description = logedUserProfile.description;
		}
		
		
	
	}//end of class
}
